local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local ConnectClass=_G.CommonOHOH.load.Connect();
local function_list = ServerOHOH.function_list;
local OnlineReward_Cfg=ServerOHOH.config.OnlineReward
local ItemTool=_G.CommonOHOH.load.ItemTool();
local EventList = ServerOHOH.load.EventList();
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.Claimed={}
    self.StartTime=os.time()
	self.ItemCtrl=self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl())
    self.DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())


    
	self.parent.RobuxBuyDoneEvent:Connect(function(gid,gtype)
		if gtype=="online_all" then
            self:BuyAllReward()
        end
	end)


end;

function M.dtor(self)
	M.super.dtor(self);
end;

function M.Claim(self,id)
    local result=false;
    if self.Claimed[id] then
        result=false;
        else
            local cfg=OnlineReward_Cfg[id]
            if os.time()- self.StartTime> cfg.time then
                local item_id,c= ItemTool.GetRewardByLevel(cfg.item_id,cfg.count,self.DataBase:GetData("base").level)
                self.Claimed[id]=true
                self.ItemCtrl:AddAnyThing(item_id,c)
                result= true
                if #self.Claimed==#OnlineReward_Cfg then
                    self:Fire(EventList.restart_reward);
                    self.Claimed={}
                    self.StartTime=os.time() 
                end
            end
    end
    return result
end
function M.FreeClaim(self,ids)
    for k, v in pairs(ids) do
        local cfg=OnlineReward_Cfg[v];
        local item_id,c= ItemTool.GetRewardByLevel(cfg.item_id,cfg.count,self.DataBase:GetData("base").level);
        self.Claimed[v]=true;
        self.ItemCtrl:AddThingsCarePack(item_id,c,false,true);
    end
    if #self.Claimed==#OnlineReward_Cfg then
        self:Fire(EventList.restart_reward);
        self.Claimed={}
        self.StartTime=os.time() 
    end
end
function M.BuyAllReward(self)
    --local items={};
    for k, value in pairs(OnlineReward_Cfg) do
        local id,c= ItemTool.GetRewardByLevel(value.item_id,value.count,self.DataBase:GetData("base").level);
        -- if not items[id] then
        --     items[id]=c;
        -- else
        --     items[id]=items[id]+c;--累加
        -- end
        self.ItemCtrl:AddThingsCarePack(id,c,false,true);
    end
    --self.ItemCtrl:AddItem(items);
    
    self:Fire(EventList.restart_reward);
    self.Claimed={}
    self.StartTime=os.time();
    return true;  
end;
M:RegistFunction(function_list.online_claim,M.Claim)
M:RegistFunction(function_list.online_freeclaim,M.FreeClaim)
return M